package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.OrderProductRepairEntity;
import com.zhenwei.zscm.mapper.IOrderProductRepairMapper;
import com.zhenwei.zscm.mappings.OrderProductRepairMapping;
import com.zhenwei.zscm.model.dto.order.OrderProductRepairDTO;
import com.zhenwei.zscm.model.vo.orderProductRepair.OrderProductRepairVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author DuckyYang
* @description 针对表【order_product_repair】的数据库操作Service实现
* @createDate 2025-05-22 09:40:59
*/
@Service
public class OrderProductRepairRepo extends ServiceImpl<IOrderProductRepairMapper, OrderProductRepairEntity> {

    /**
     * 添加一条工单维修数据
     * @param vo vo
     * @return bool
     */
    public boolean create(OrderProductRepairVO vo) {
        OrderProductRepairEntity e = OrderProductRepairMapping.INSTANCE.vo2Entity(vo);
        e.setGuid(IdUtil.simpleUUID());
        return this.save(e);
    }

    /**
     * 获取工单产品维修数据
     * @param orderId 工单id
     * @param productSn 产品sn
     * @return OrderProductRepairDTO
     */
    public OrderProductRepairDTO detail(String orderId, String productSn) {
        var q = new LambdaQueryWrapper<OrderProductRepairEntity>()
                .eq(OrderProductRepairEntity::getOrderId, orderId)
                .eq(OrderProductRepairEntity::getProductSn, productSn)
                .eq(OrderProductRepairEntity::getDeleted,false);
        var list = this.list(q);
        return Linq.getFirst(list, OrderProductRepairMapping.INSTANCE::entity2Dto);
    }

    /**
     * 修改工单产品维修数据
     * @param vo vo
     * @return bool
     */
    public boolean update(OrderProductRepairVO vo) {
        var u = new LambdaUpdateWrapper<OrderProductRepairEntity>()
                .eq(OrderProductRepairEntity::getOrderId, vo.getOrderId())
                .eq(OrderProductRepairEntity::getProductSn, vo.getProductSn())
                .eq(OrderProductRepairEntity::getDeleted,false)
                .set(OrderProductRepairEntity::getPayState, vo.getPayState())
                .set(OrderProductRepairEntity::getPayDate, vo.getPayDate())
                .set(OrderProductRepairEntity::getPayTypeCode, vo.getPayTypeCode())
                .set(OrderProductRepairEntity::getPayType, vo.getPayType())
                .set(OrderProductRepairEntity::getIsInvoice, vo.getIsInvoice())
                .set(OrderProductRepairEntity::getInvoiceTypeCode, vo.getInvoiceTypeCode())
                .set(OrderProductRepairEntity::getInvoiceType, vo.getInvoiceType())
                .set(OrderProductRepairEntity::getInvoiceNumber, vo.getInvoiceNumber())
                .set(OrderProductRepairEntity::getInvoiceDate, vo.getInvoiceDate())
                .set(OrderProductRepairEntity::getReturnDate, vo.getReturnDate())
                .set(OrderProductRepairEntity::getReturnNumber, vo.getReturnNumber())
                .set(OrderProductRepairEntity::getRepairStateCode, vo.getRepairStateCode())
                .set(OrderProductRepairEntity::getRepairState, vo.getRepairState());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    public boolean delete(String orderId) {
        var u = new LambdaUpdateWrapper<OrderProductRepairEntity>()
                .eq(OrderProductRepairEntity::getOrderId, orderId)
                .eq(OrderProductRepairEntity::getDeleted,false)
                .set(OrderProductRepairEntity::getDeleted,true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    public List<OrderProductRepairDTO> getList(String orderId) {
        var q = new LambdaQueryWrapper<OrderProductRepairEntity>()
                .eq(OrderProductRepairEntity::getOrderId, orderId)
                .eq(OrderProductRepairEntity::getDeleted,false)
                .orderByAsc(OrderProductRepairEntity::getId);
        var list = this.list(q);
        return Linq.mapList(list, OrderProductRepairMapping.INSTANCE::entity2Dto);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<OrderProductRepairDTO> orderProductRepairList) {
        var entityList = Linq.mapList(orderProductRepairList, OrderProductRepairMapping.INSTANCE::dto2Entity);
        return this.saveBatch(entityList);
    }
}




